package com.fyzk.activity.lottery.togetherbuy;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tyjc.lottery.R;

/**
 * Created by qinkang on 2018/7/30.
 */

public class HM_VerticalPop extends PopupWindow implements View.OnTouchListener {

    private Context context;
    private String[] items;
    private View view;
    private ListView gv;
    private int checkItem = 1;
    private HM_PopuponItemListener listener;
    private HM_PopupAdapter adapter;
    private LinearLayout mLlView;
    private TextView mAll;
    private TextView mOneWeek;
    private TextView mOneMonth;
    private TextView mThreeMonth;

    @Deprecated
    public HM_VerticalPop(Context context, View view, int w, int h, String[] items) {
        super(view, w, h);
        this.setContentView(view);
        this.setContentView(view);
        this.context = context;
        this.view = view;
        this.items = items;
        initView(items);
        this.setContentView(view);
        this.setWidth(w);
        this.setHeight(h);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        ColorDrawable dw = new ColorDrawable(0000000000);
        this.setBackgroundDrawable(dw);
        this.setAnimationStyle(R.style.showPopupAnimation);
    }

    public HM_VerticalPop(Context context, int w, int h, String[] items) {
        this(context, w, h, items, true);
    }

    public HM_VerticalPop(Context context, int w, int h, String[] items, boolean isTouchOutside) {
        super(w, h);
        this.context = context;
        View view_pop = LayoutInflater.from(context).inflate(R.layout.hm_verticalpop, null);
        this.view = view_pop;
        this.view.setOnTouchListener(this);
        this.items = items;
        this.setContentView(view);
        initView(items);
        this.setFocusable(isTouchOutside);
        this.setOutsideTouchable(isTouchOutside);
        // 刷新状态
        this.update();
        ColorDrawable dw = new ColorDrawable(0000000000);
        this.setBackgroundDrawable(dw);
        this.setAnimationStyle(R.style.showPopupAnimation);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        this.dismiss();
        return true;
    }

    private void initView(String[] items) {
        mAll = view.findViewById(R.id.all);
        mOneWeek = view.findViewById(R.id.one_week);
        mOneMonth = view.findViewById(R.id.one_month);
        mThreeMonth = view.findViewById(R.id.three_month);

        mAll.setText(items[0]);
        mOneWeek.setText(items[1]);
        mOneMonth.setText(items[2]);
        mThreeMonth.setText(items[3]);

        if (checkItem == 0) {
            mAll.setTextColor(Color.parseColor("#FD4A4A"));
        } else if (checkItem == 1) {
            mOneWeek.setTextColor(Color.parseColor("#FD4A4A"));
        } else if (checkItem == 2) {
            mOneMonth.setTextColor(Color.parseColor("#FD4A4A"));
        } else if (checkItem == 3) {
            mThreeMonth.setTextColor(Color.parseColor("#FD4A4A"));
        }

        mAll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (checkItem != 0 && null != listener) {
                    checkItem = 0;
                    listener.onItem(0);
                }
                popupDismiss();
            }
        });
        mOneWeek.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (checkItem != 1 && null != listener) {
                    checkItem = 1;
                    listener.onItem(1);
                }
                popupDismiss();
            }
        });
        mOneMonth.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (checkItem != 2 && null != listener) {
                    checkItem = 2;
                    listener.onItem(2);
                }
                popupDismiss();
            }
        });
        mThreeMonth.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (checkItem != 3 && null != listener) {
                    checkItem = 3;
                    listener.onItem(3);
                }
                popupDismiss();
            }
        });
    }


    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
//            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
            if (null != listener) {
                listener.popupShow();
                initText();
            }
            super.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }

    private void initText() {
        if (checkItem == 0) {
            mAll.setTextColor(Color.parseColor("#FD4A4A"));
            mOneWeek.setTextColor(Color.parseColor("#111111"));
            mOneMonth.setTextColor(Color.parseColor("#111111"));
            mThreeMonth.setTextColor(Color.parseColor("#111111"));
        } else if (checkItem == 1) {
            mOneWeek.setTextColor(Color.parseColor("#FD4A4A"));
            mAll.setTextColor(Color.parseColor("#111111"));
            mOneMonth.setTextColor(Color.parseColor("#111111"));
            mThreeMonth.setTextColor(Color.parseColor("#111111"));
        } else if (checkItem == 2) {
            mOneMonth.setTextColor(Color.parseColor("#FD4A4A"));
            mAll.setTextColor(Color.parseColor("#111111"));
            mOneWeek.setTextColor(Color.parseColor("#111111"));
            mThreeMonth.setTextColor(Color.parseColor("#111111"));
        } else if (checkItem == 3) {
            mThreeMonth.setTextColor(Color.parseColor("#FD4A4A"));
            mAll.setTextColor(Color.parseColor("#111111"));
            mOneMonth.setTextColor(Color.parseColor("#111111"));
            mOneWeek.setTextColor(Color.parseColor("#111111"));
        }
    }

    private void popupDismiss() {
        this.dismiss();
    }

    @Override
    public void dismiss() {
        if (null != listener) {
            listener.popupDismiss();
        }
        super.dismiss();
    }

    public void setCheckItem(int checkItem) {
        this.checkItem = checkItem;
    }

    public int getCheckItem() {
        return this.checkItem;
    }

    //===== 接口	==========
    public interface HM_PopuponItemListener {
        void onItem(int position);

        void popupShow();

        void popupDismiss();
    }

    public void setHm_PopupOnItemListener(HM_PopuponItemListener listener) {
        this.listener = listener;
    }

    public void setItems(String[] items) {
        this.items = items;
        adapter.setItems(items);
    }

    //==========================================================================
    class HM_PopupAdapter extends BaseAdapter {
        private String[] items;

        public HM_PopupAdapter(String[] items) {
            this.items = items;
        }

        @Override
        public int getCount() {

            return items.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            String item = items[position];
            convertView = LayoutInflater.from(context).inflate(R.layout.hm_verticalpop_item, null);
            TextView text = (TextView) convertView.findViewById(R.id.hm_popup_item_tv);
            LinearLayout ll = (LinearLayout) convertView.findViewById(R.id.ll);

            text.setText(item);
            if (checkItem == position) {
                text.setTextColor(Color.parseColor("#FD4A4A"));
            } else {
                text.setTextColor(Color.parseColor("#111111"));
            }

            text.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (checkItem != position && null != listener) {
                        checkItem = position;
                        listener.onItem(position);
                    }

                    notifyDataSetChanged();
                    popupDismiss();
                }
            });

            return convertView;
        }

        public void setItems(String[] items) {
            this.items = items;
            this.notifyDataSetChanged();
        }

    }


//
//    public void setNumColumns(int numColumns){
//        this.gv.setNumColumns(numColumns);
//    }


}
